package kr.hkit.team.score.service;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import kr.hkit.team.score.dao.ScoreDao;
import kr.hkit.team.score.dto.Score;

import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ListDlg extends JDialog {

	private ScoreDao dao;
	private ArrayList<Score> lists;
	private final JPanel contentPanel = new JPanel();
	private String[] colNames = {"등급", "순위", "학번", "성명", "국어", "영어", "수학", "총점", "평균"};
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ListDlg dialog = new ListDlg();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ListDlg() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane);
			{
				table = new JTable(new String[0][0], colNames);
				scrollPane.setViewportView(table);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public void loadData() {
		try {
			lists = dao.setrank();
			int len = lists.size();
			arLists = new String[len][colNames.length];
			for (int i = 0; i < len; i++) {
				Score temp = lists.get(i);
				arLists[i] = temp.toArray();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}

}
